package io.netty.myExample.zhanglong.p7客户端和服务端连接;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

/**
 * netty初始化器，channel 注册之后，会执行里面相应的初始化方法
 */
public class TestInitializer2 extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //添加字符解码器
        //如果没有指定名字，则会默认为类的名字
        pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
        pipeline.addLast(new LengthFieldPrepender(4));
        pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
        //添加加密器
        pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
        //添加服务器自定义处理器
        pipeline.addLast(new TestHttpServerHandler2());

    }
}
